Page({
  data: {
    arrangement: '',

    // study数据
    weeks: [
      { text: '日' },{ text: '一' },{ text: '二' },{ text: '三' },
      { text: '四' },{ text: '五' },{ text: '六' },
    ],
    show: false,
    selectedDate: { 
      year: new Date().getFullYear(),
      month: new Date().getMonth() + 1,
      date: new Date().getDate()
    },
    currentYear: new Date().getFullYear(),
    years: [],
    currentMonthIndex: 0,
    months: [],

    // exam数据
    exam: [
      {
        title: '考试（第一学期）',
        dates: [
          { id: 1, name: '专业课考试周（18周）', days: '1.29 - 1.4'},
          { id: 2, name: '公共课考试周（19周）', days: '1.5 - 1.11'},
        ]
      },
      {
        title: '考试（第二学期）',
        dates: [
          { id: 1, name: '专业课考试周（17周）', days: '6.16 - 6.22'},
          { id: 2, name: '公共课考试周（18周）', days: '6.23 - 6.29'},
        ]
      },
      {
        title: '暑期小学期',
        dates: [
          { id: 1, name: '大一', days: '6.29 - 7.5 [约7天]'},
          { id: 2, name: '大二', days: '6.29 - 7.12 [约14天]'},
          { id: 3, name: '大三', days: '6.29 - 7.26 [约28天]'},
        ]
      }
    ],

    // holiday数据
    holiday: [
      {
        title: "2024.9.1 - 2025.1.11（第一学期）",
        dates: [
          { id: 1, name: '中秋节', days: '9.17 [1天]'},
          { id: 2, name: '国庆节', days: '10.1-10.7 [7天]'},
          { id: 3, name: '元旦', days: '1.1 [1天]'},
          { id: 4, name: '寒假', days: '1.12-2.22 [42天]'},
        ]
      },
      {
        title: "2025.2.23 - 2025.6.28（第二学期）",
        dates: [
          { id: 1, name: '清明节', days: '4.4 [1天]'},
          { id: 2, name: '运动会', days: '2.28-2.29 [2天]'},
          { id: 3, name: '劳动节', days: '5.1-5.5 [5天]'},
          { id: 4, name: '端午节', days: '5.31-6.2 [3天]'},
          { id: 5, name: '暑假', days: '7.27-9.6 [42天]'},

        ]
      },
    ]

  },
  onLoad(options) {
    this.initYearSelector();   
    this.generateCalendar(); 
    this.setData({
      arrangement: options.type,
    }); 
  },

  // study日历方法逻辑
  initYearSelector() {
    const currentYear = this.data.currentYear;
    const years = [];
    for (let i = currentYear - 10; i <= currentYear + 10; i++) {
      years.push(i);
    }
    this.setData({
      years: years
    });
  },
  generateCalendar() {
    const currentYear = this.data.currentYear;
    const months = [];
    for (let month = 0; month < 12; month++) {
      const days = [];
      const date = new Date(currentYear, month, 1);
      const firstDayOfWeek = date.getDay();
      const daysInMonth = new Date(currentYear, month + 1, 0).getDate();
      for (let i = 0; i < firstDayOfWeek; i++) {
        const prevDate = new Date(prevYear, prevMonth, prevMonthDays - firstDayOfWeek + i + 1)
        const dayOfWeek = prevDate.getDay()  
        const prevMonth = month === 0 ? 11 : month - 1;
        const prevYear = month === 0 ? currentYear - 1 : currentYear;
        const prevMonthDays = new Date(prevYear, prevMonth + 1, 0).getDate();
        days.push({
          date: prevMonthDays - firstDayOfWeek + i + 1,
          isCurrentMonth: false,
          month: prevMonth + 1,
          year: prevYear,
          isWeekend: dayOfWeek === 0 || dayOfWeek === 6
        });
      }
      for (let i = 1; i <= daysInMonth; i++) {
        const dayOfWeek = new Date(currentYear, month, i).getDay();
        days.push({
          date: i,
          isCurrentMonth: true,
          month: month + 1,
          year: currentYear,
          isWeekend: dayOfWeek === 0 || dayOfWeek === 6
        });
      }
      const totalCells = 42; 
      const remainingCells = totalCells - days.length;
      for (let i = 0; i < remainingCells; i++) {
        const nextMonth = month === 11 ? 0 : month + 1;
        const nextYear = month === 11 ? currentYear + 1 : currentYear;
        const dayOfWeek = new Date(nextYear, nextMonth, i + 1).getDay();
        days.push({
          date: i + 1,
          isCurrentMonth: false,
          month: nextMonth + 1,
          year: nextYear,
          isWeekend: dayOfWeek === 0 || dayOfWeek === 6
        });
      }
      months.push({
        year: currentYear,
        month: month + 1,
        days: days
      });
    }
    this.setData({
      months: months
    });
  },
  yearChange(e) {
    const selectedYearIndex = e.detail.value;
    const selectedYear = this.data.years[selectedYearIndex];
    this.setData({
      currentYear: selectedYear,
      currentMonthIndex: 0
    }, () => {
      this.generateCalendar();
    });
  },
  swiperChange(e) {
    this.setData({
      currentMonthIndex: e.detail.current
    });
  },
  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  onDateClick(e) { 
    const { year, month, date } = e.currentTarget.dataset;  
    const selected = {
      year: Number(year),
      month: Number(month),
      date: Number(date)
    };
    this.setData({  
      selectedDate: selected,
      show: true 
    }); 
  },

  onReady() {

  },
  onShow() {

  },
  onHide() {

  },
  onUnload() {

  },
  onPullDownRefresh() {

  },tom() {

  },
  onShareAppMessage() {
  }
})